package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import com.example.myapplication.utils.Request;
import com.example.myapplication.utils.UICache;

import org.json.JSONException;
import org.json.JSONObject;

public class Seach extends AppCompatActivity {
    private SearchView searchView;
    private ListView listView;
    JSONObject json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seach);

        listView = (ListView) findViewById(R.id.lv);
        ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);
        searchView = (SearchView) findViewById(R.id.sv);
        //设置SearchView自动缩小为图标
        searchView.setIconifiedByDefault(false);//设为true则搜索栏 缩小成俄日一个图标点击展开
        //设置该SearchView显示搜索按钮
        searchView.setSubmitButtonEnabled(true);
        //设置默认提示文字
        searchView.setQueryHint("输入您想查找的城市名");
        //配置监听器
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //点击搜索按钮时触发
            @Override
            public boolean onQueryTextSubmit(String query) {
                //此处添加查询开始后的具体时间和方法
                //Toast.makeText(Seach.this,"you choose:" + query,Toast.LENGTH_SHORT).show();
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                try {
                    adapter.clear();
                    json = Request.json("http://pc.weathercn.com/search?q="+newText);
                    for (int i = 0; i < json.getJSONArray("data").length(); i++) {
                        adapter.add(json.getJSONArray("data").getJSONObject(i).getString("LocalizedName")+" "+json.getJSONArray("data").getJSONObject(i).getJSONObject("AdministrativeArea").getString("LocalizedName"));
                    }
                    //如果newText长度不为0
                    if (TextUtils.isEmpty(newText)){
                        listView.clearTextFilter();
                    }else{
                        listView.setFilterText(newText);
                    //adapter.getFilter().filter(newText.toString());//替换成本句后消失黑框！！！
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return true;
            }
        });
        listView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    String value = json.getJSONArray("data").getJSONObject(position).getString("Key");

                    Request.pool.submit(new Runnable() {
                        @Override
                        public void run() {
                            UICache.city=value;
                            //存储数据
                            SharedPreferences.Editor editor = getSharedPreferences("share", MODE_PRIVATE).edit();
                            editor.putString("city",UICache.city);
                            editor.commit();
                        }
                    });
                    //Intent intent = getIntent();
                    //intent.putExtra("city",value);
                    //setResult(5656,intent);
                    finish();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}